package zozopo.warehouse.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class ChangewarehouserecordOS extends OperatingService{
	
	
	public ChangewarehouserecordOS(Connection conn){
		super(conn);
	}
	
	public ChangewarehouserecordOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ChangewarehouserecordTB> selectListBySql(String sql){
		List<ChangewarehouserecordTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		//Flow flow = new Flow(conn);
		//Map flowMap = flow.getFlowStateMapByKind(FlowCode.HOUSEWARE_RECEIPTLIST);
		if(resultSet != null){
				try {
					list = new ArrayList<ChangewarehouserecordTB>();
					while(resultSet.next()){
						ChangewarehouserecordTB obj = new ChangewarehouserecordTB();
						
						
						obj.setDate_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.DATE_CHANGEWAREHOUSERECORD));
						obj.setGoodsid_changewarehouserecord(resultSet.getInt(ChangewarehouserecordTB.GOODSID_CHANGEWAREHOUSERECORD));
						obj.setGoodsname_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.GOODSNAME_CHANGEWAREHOUSERECORD));
						obj.setGoodunit_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.GOODUNIT_CHANGEWAREHOUSERECORD));
						obj.setId_changewarehouserecord(resultSet.getInt(ChangewarehouserecordTB.ID_CHANGEWAREHOUSERECORD));
						obj.setNumber_changewarehouserecord(resultSet.getInt(ChangewarehouserecordTB.NUMBER_CHANGEWAREHOUSERECORD));
						obj.setRemark_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.REMARK_CHANGEWAREHOUSERECORD));
						obj.setState_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.STATE_CHANGEWAREHOUSERECORD));
						
						obj.setTargetwarehouseid_changewarehouserecord(resultSet.getInt(ChangewarehouserecordTB.TARGETWAREHOUSEID_CHANGEWAREHOUSERECORD));
						obj.setTargetwarehousename_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.TARGETWAREHOUSENAME_CHANGEWAREHOUSERECORD));
						
						obj.setUseremail_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.USEREMAIL_CHANGEWAREHOUSERECORD));
						obj.setUsername_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.USERNAME_CHANGEWAREHOUSERECORD));
						
						obj.setWarehouseid_changewarehouserecord(resultSet.getInt(ChangewarehouserecordTB.WAREHOUSEID_CHANGEWAREHOUSERECORD));
						obj.setWarehousename_changewarehouserecord(resultSet.getString(ChangewarehouserecordTB.WAREHOUSENAME_CHANGEWAREHOUSERECORD));
						
						list.add(obj);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(ChangewarehouserecordTB changewarehouserecord){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		insertMap.put(ChangewarehouserecordTB.GOODSID_CHANGEWAREHOUSERECORD, changewarehouserecord.getGoodsid_changewarehouserecord());
		insertMap.put(ChangewarehouserecordTB.GOODSNAME_CHANGEWAREHOUSERECORD, changewarehouserecord.getGoodsname_changewarehouserecord());
		//insertMap.put(ChangewarehouserecordTB.DATE_CHANGEWAREHOUSERECORD, changewarehouserecord.getDate_changewarehouserecord());
		insertMap.put(ChangewarehouserecordTB.NUMBER_CHANGEWAREHOUSERECORD, changewarehouserecord.getNumber_changewarehouserecord());
		insertMap.put(ChangewarehouserecordTB.REMARK_CHANGEWAREHOUSERECORD, changewarehouserecord.getRemark_changewarehouserecord());
		
		insertMap.put(ChangewarehouserecordTB.TARGETWAREHOUSEID_CHANGEWAREHOUSERECORD, changewarehouserecord.getTargetwarehouseid_changewarehouserecord());
		insertMap.put(ChangewarehouserecordTB.TARGETWAREHOUSENAME_CHANGEWAREHOUSERECORD, changewarehouserecord.getTargetwarehousename_changewarehouserecord());
		
		insertMap.put(ChangewarehouserecordTB.WAREHOUSEID_CHANGEWAREHOUSERECORD, changewarehouserecord.getWarehouseid_changewarehouserecord());
		insertMap.put(ChangewarehouserecordTB.WAREHOUSENAME_CHANGEWAREHOUSERECORD, changewarehouserecord.getWarehousename_changewarehouserecord());
		
		
		insertMap.put(ChangewarehouserecordTB.USEREMAIL_CHANGEWAREHOUSERECORD, changewarehouserecord.getUseremail_changewarehouserecord());
		insertMap.put(ChangewarehouserecordTB.USERNAME_CHANGEWAREHOUSERECORD, changewarehouserecord.getUsername_changewarehouserecord());
		
	
		
		return sqlMachine.insertRow(ChangewarehouserecordTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxIdByEmail(String email){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(ChangewarehouserecordTB.ID_CHANGEWAREHOUSERECORD, ChangewarehouserecordTB.TABLE_NAME, ChangewarehouserecordTB.USEREMAIL_CHANGEWAREHOUSERECORD, email);
		return maxId;
	}
	
	public ChangewarehouserecordTB get(int id){
		String sql = "SELECT * FROM " + ChangewarehouserecordTB.TABLE_NAME + " where " + ChangewarehouserecordTB.ID_CHANGEWAREHOUSERECORD + "=" + id;
		ChangewarehouserecordTB obj = new ChangewarehouserecordTB();
		List<ChangewarehouserecordTB> list = selectListBySql(sql);
		obj = list.get(0);
		return obj;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ChangewarehouserecordTB> selectAll(){
		String sql = "SELECT * FROM " + ChangewarehouserecordTB.TABLE_NAME;
		List<ChangewarehouserecordTB> list =  selectListBySql(sql);
		
		return list;
	}
	
	public List<ChangewarehouserecordTB> selectAll(int beginPageNum,int pageSize){
		
		List<ChangewarehouserecordTB> list = null;
		String sql = "SELECT * FROM " + ChangewarehouserecordTB.TABLE_NAME  
		+ " ORDER BY " + ChangewarehouserecordTB.DATE_CHANGEWAREHOUSERECORD + " DESC "
		+ " limit "+beginPageNum+","+pageSize; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + ChangewarehouserecordTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<ChangewarehouserecordTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		String sql = "select * from "+ChangewarehouserecordTB.TABLE_NAME+" WHERE  "
		+ ChangewarehouserecordTB.GOODSNAME_CHANGEWAREHOUSERECORD+" LIKE '%"+searchStr+"%'"
		+" OR "+ChangewarehouserecordTB.GOODSID_CHANGEWAREHOUSERECORD+" LIKE '%"+searchStr+"%'"
		+" OR "+ChangewarehouserecordTB.GOODUNIT_CHANGEWAREHOUSERECORD+" LIKE '%"+searchStr+"%'"
		+" OR "+ChangewarehouserecordTB.WAREHOUSENAME_CHANGEWAREHOUSERECORD+" LIKE '%"+searchStr+"%'"
		+" limit "+beginPageNum+","+pageSize; 
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		return selectListBySql(sql);
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + GoodsTB.TABLE_NAME +" WHERE  "
		+ ChangewarehouserecordTB.GOODSNAME_CHANGEWAREHOUSERECORD+" LIKE '%"+searchStr+"%'"
		+" OR "+ChangewarehouserecordTB.GOODSID_CHANGEWAREHOUSERECORD+" LIKE '%"+searchStr+"%'"
		+" OR "+ChangewarehouserecordTB.GOODUNIT_CHANGEWAREHOUSERECORD+" LIKE '%"+searchStr+"%'"
		+" OR "+ChangewarehouserecordTB.WAREHOUSENAME_CHANGEWAREHOUSERECORD+" LIKE '%"+searchStr+"%'";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}

}
